﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.DataTransferObject.Query;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Interface;
using Sharp.Portal.Mvc;
using Sharp.Portal.Mvc.List;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.OP.Models;


namespace Sharp.WebBooter.Areas.OP.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new IndexViewModel();


            return View(model);
        }

        public ActionResult Do(bool? initPermission, bool? allotToAdminRole)
        {
            if (initPermission.HasValue && initPermission.Value)
            {
                List<PermissionDTO> list = new List<PermissionDTO>();
                Assembly assembly = this.GetType().Assembly;
                foreach (var type in assembly.GetTypes())
                {
                    if (type.GetInterface(nameof(INavigation)) != null)
                    {
                        var navigation = Activator.CreateInstance(type) as INavigation;
                        list.AddRange(navigation.Navigation());
                    }
                }

                PermissionScan ps = new PermissionScan();


                list = ps.GetAllActionByAssembly(list, assembly).ToList();

                StringBuilder sb = new StringBuilder();
                foreach (var item in list.Where(x => x.ForFieldPermission != null))
                {
                    sb.AppendFormat("{0}\r{1}\r", item.Code + item.SystemCode,
                        string.Join("&",
                            ((List<ColumnItem>)item.ForFieldPermission).Where(i => !i.Hidden)
                            .Select(i => i.Text + ":" + i.DataIndex)));
                }
                Result res;
                using (var proxy = new WebApiClientServiceProxy<IUserService>())
                {
                    res = proxy.Channel.RefurbishAvailablePermissionData(new PermissionDataQuery()
                    {
                        Dtos = list,
                        Columns = sb.ToString()
                    });
                }

                if (res.Err.Length > 0)
                {
                    ModelState.AddModelError("", res.Err);
                    return View("Index");
                }

                ConfigurationManager.AppSettings["InitPermissionData"] = "false";

            }

            if (allotToAdminRole.HasValue && allotToAdminRole.Value)
            {
                Result res;
                using (var proxy = new WebApiClientServiceProxy<IUserService>())
                {
                    res = proxy.Channel.AllotAllPermissionToAdminRole();
                }
                if (res.Err.Length > 0)
                {
                    ModelState.AddModelError("", res.Err);
                    return View("Index");
                }
            }

            return View("Index");
        }
    }
}